# -*- coding: utf-8 -*-
"""
    The following copyright and license notice apply only to:
        - those parts of this Turbogears 2 project which are not
          part of the stock quickstarted project
        - those parts which are not otherwise licensed otherwise

    Copyright (C) 2009 Ciaran Farrell <cfarrell1980@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from tgext.admin import AdminController
from tg import expose, flash, require, url, request, redirect,tmpl_context
from pylons.i18n import ugettext as _, lazy_ugettext as l_
from pylons.controllers.util import etag_cache
#from catwalk.tg2 import Catwalk
from repoze.what import predicates

from lifepyscle.lib.base import BaseController
from lifepyscle.model import DBSession, metadata
from lifepyscle.controllers.error import ErrorController
from lifepyscle import model
from lifepyscle.controllers.secure import SecureController
# import the other controllers (they are in external files)
from lifepyscle.controllers.record import RecordController
from lifepyscle.controllers.party import PartyController
from lifepyscle.controllers.document import DocumentController
from lifepyscle.controllers.event import EventController
from lifepyscle.controllers.reminder import ReminderController
from lifepyscle.controllers.term import TermController
from lifepyscle.controllers.eventtype import EventTypeController
__all__ = ['RootController']

cat="root"
class RootController(BaseController):
    """
    The root controller for the lifepyscle application.
    
    All the other controllers and WSGI applications should be mounted on this
    controller. For example::
    
        panel = ControlPanelController()
        another_app = AnotherWSGIApplication()
    
    Keep in mind that WSGI applications shouldn't be mounted directly: They
    must be wrapped around with :class:`tg.controllers.WSGIAppController`.
    
    """
    secc = SecureController()
    
    #admin = Catwalk(model, DBSession)
    #admin = AdminController(model,DBSession)
    error = ErrorController()
    record = RecordController()
    party = PartyController()
    document = DocumentController()
    event = EventController()
    reminder = ReminderController()
    term = TermController()
    eventtype = EventTypeController()

    @expose('lifepyscle.templates.index')
    def index(self):
        """Handle the front-page."""
        from lifepyscle.widgets.add_recips import add_recips_form
        tmpl_context.kcbk = add_recips_form
        return dict(page='index',cat=cat)

    @expose('lifepyscle.templates.dashboard')
    def dashboard(self):
        """Handle the dashboard."""
        return dict(page='dashboard',cat=cat)

    @expose('lifepyscle.templates.test')
    def test(self):
        """Handle the test."""
        return dict(page='test',cat=cat)

    @expose('lifepyscle.templates.about')
    def about(self):
        """Handle the 'about' page."""
        return dict(page='about',cat=cat)

    @expose('lifepyscle.templates.authentication')
    def auth(self):
        """Display some information about auth* on this application."""
        return dict(page='auth',cat=cat)

    @expose('lifepyscle.templates.index')
    @require(predicates.has_permission('manage', msg=l_('Only for managers')))
    def manage_permission_only(self, **kw):
        """Illustrate how a page for managers only works."""
        return dict(page='managers stuff',cat=cat)

    @expose('lifepyscle.templates.index')
    @require(predicates.is_user('editor', msg=l_('Only for the editor')))
    def editor_user_only(self, **kw):
        """Illustrate how a page exclusive for the editor works."""
        return dict(page='editor stuff',cat=cat)

    @expose('lifepyscle.templates.login')
    def login(self, came_from=url('/')):
        """Start the user login."""
        login_counter = request.environ['repoze.who.logins']
        if login_counter > 0:
            flash(_('Wrong credentials'), 'warning')
        return dict(page='login', login_counter=str(login_counter),
                    came_from=came_from,cat=cat)
    
    @expose()
    def post_login(self, came_from=url('/')):
        """
        Redirect the user to the initially requested page on successful
        authentication or redirect her back to the login page if login failed.
        
        """
        if not request.identity:
            login_counter = request.environ['repoze.who.logins'] + 1
            redirect(url('/login', came_from=came_from, __logins=login_counter))
        userid = request.identity['repoze.who.userid']
        flash(_('Welcome back, %s!') % userid)
        redirect(came_from)

    @expose()
    def post_logout(self, came_from=url('/')):
        """
        Redirect the user to the initially requested page on logout and say
        goodbye as well.
        
        """
        flash(_('We hope to see you soon!'))
        redirect(came_from)
